package com.yanqu.road.dao.impl.activity.hsisland;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandKillBossRecord;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HsIslandKillBossRecordDaoImpl extends TempDataObjectDao<HsIslandKillBossRecord> {

    public static String getHsIslandKillBossRecordMapSql = "select * from `t_c_activity_hs_island_kill_boss_record` where `activityId`=? and `groupId`=? ";

    public Map<Long, HsIslandKillBossRecord> getHsIslandKillBossRecordMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Long, HsIslandKillBossRecord> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandKillBossRecordMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandKillBossRecord data = parse(rs);
                map.put(data.getBattleFieldId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateHsIslandKillBossRecordSql = "update `t_c_activity_hs_island_kill_boss_record` set monsterId=?,killUserId=?,killTime=? where activityId=? and groupId=? and battleFieldId=? ";
    @Override
    protected boolean updateData(HsIslandKillBossRecord record) {
        record.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, record.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, record.getKillUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, record.getKillTime()));
        para.put(i++, new DbParameter(Types.INTEGER, record.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, record.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, record.getBattleFieldId()));
        if (getDb().executeNoneQuery(updateHsIslandKillBossRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandKillBossRecordSql = "insert into `t_c_activity_hs_island_kill_boss_record`(activityId,groupId,battleFieldId,monsterId,killUserId,killTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandKillBossRecord record) {
        record.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, record.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, record.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, record.getBattleFieldId()));
        para.put(i++, new DbParameter(Types.INTEGER, record.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, record.getKillUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, record.getKillTime()));
        if (getDb().executeNoneQuery(insertHsIslandKillBossRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandKillBossRecord parseData(ResultSet rs) throws SQLException {
        HsIslandKillBossRecord record = new HsIslandKillBossRecord();
        record.setActivityId(rs.getInt("activityId"));
        record.setGroupId(rs.getInt("groupId"));
        record.setMonsterId(rs.getInt("monsterId"));
        record.setBattleFieldId(rs.getLong("battleFieldId"));
        record.setKillUserId(rs.getLong("killUserId"));
        record.setKillTime(rs.getLong("killTime"));
        return record;
    }
}
